<?php
/**
 * Created by PhpStorm.
 * User: 13064
 * Date: 2018/12/1
 * Time: 11:39
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
class Login extends Controller
{
    public function index()
    {
        return view('login/login');
    }

    public function login()
    {
        $p = input('post.');
        $p['pass'] = md5($p['pass']);
        $row = Db::table('club_adminn')->where($p)->field('pass',true)->find();
    
        if (!empty($row)){
            $uid = $row['id'];

            Db::name('club_adminn')->where('id',$uid)->update($row);

            $res = Db::table('club_adminn')
                ->alias('cu')
                ->join('club_admin_role car', 'cu.id = aid','INNER')
                ->join('club_adminrole cr', 'car.rid = cr.id')
                ->join('club_role_range crr', 'cr.id = crr.rid')
                ->join('club_adminrange sn', 'crr.mid = sn.id')
                ->where('cu.id', $row['id'])
                ->field('moduleName, controllerName, method')
                ->group('moduleName, controllerName, method')
                ->select();
//            var_dump($res, $row);
            $list = array();
            foreach ($res as $k => $v){
                if (empty($list)) {
                    $list[$k]['moduleName'] = $res[$k]['moduleName'];
                    $list[$k]['controllerName'] = ucfirst($res[$k]['controllerName']);
                    $list[$k]['method'] = $res[$k]['method'];
                } else {
                    foreach ($list as $val) {
                        if (($val['moduleName'] == $res[$k]['moduleName']) && ($val['controllerName'] == $res[$k]['controllerName']) && ($val['method'] == $res[$k]['method'])) {
                            break;
                        } else {
                            $list[$k]['moduleName'] = $res[$k]['moduleName'];
                            $list[$k]['controllerName'] = ucfirst($res[$k]['controllerName']);
                            $list[$k]['method'] = $res[$k]['method'];
                        }
                    }
                }
            }
//            var_dump($res);die;
            $list = serialize($list);
            if (empty($res) || $res === null ) {
                $this->error('无权登录');
            } else {
                session("admin['id']", "{$row['id']}");
                session("admin['name']", "{$row['name']}");
                session("admin['access']", "{$list}");
                $this->success('登录成功！',url('admin/index/index'));
            }
        }else {
            $this->error('您的用户名或密码输入有误，请重新输入！',url('admin/login/index'));
        }
    }
    public function exitlogin()
    {
        session("admin['id']", null);
        session("admin['name']", null);
        session("admin['access']", null);
        if (!session("?admin['id']") or !session("?admin['name']")) {
            $this->success('已退出', url('admin/Login/index'));
        } else {
            $this->error('退出失败');
        }
    }
}